/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.practice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduSection;
import com.idenger.modules.edu.entity.practice.EduPractice;
import com.idenger.modules.edu.entity.questionwarehouse.EduQuestionWarehouse;
import com.idenger.modules.edu.service.course.EduSectionService;
import com.idenger.modules.edu.service.practice.EduPracticeService;
import com.idenger.modules.edu.service.practicsubject.EduPracticSubjectService;

import com.idenger.modules.edu.web.comm.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.practicsubject.EduPracticSubject;
import com.idenger.modules.edu.entity.selectiontitle.EduSelectionTitle;
import com.idenger.modules.edu.service.questionwarehouse.EduQuestionWarehouseService;
import com.idenger.modules.edu.service.selectiontitle.EduSelectionTitleService;

/**
 * 单元练习Controller
 * @author 兰正军
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/practice/eduPractice")
public class EduPracticeController extends BaseController {

	@Autowired
	private EduPracticeService eduPracticeService;
	@Autowired
	private EduPracticSubjectService eduPracticSubjectService;
	@Autowired
	private EduSelectionTitleService eduSelectionTitleService;

	@Autowired
	private EduSectionService eduSectionService;
	
	@Autowired
	private EduQuestionWarehouseService eduQuestionWarehouseService;
	
	@ModelAttribute
	public EduPractice get(@RequestParam(required=false) String id) {
		EduPractice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduPracticeService.get(id);
		}
		if (entity == null){
			entity = new EduPractice();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:practice:eduPractice:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduPractice eduPractice, HttpServletRequest request, HttpServletResponse response, Model model) {

		String sectionId = request.getParameter("sectionId");
		if(StringUtils.isNotBlank(sectionId)){
			eduPractice.setSectionId(null);
		}
		eduPractice.setRemarks(Constant.PRACTICE_TEMPLATE_YES);
		Page<EduPractice> page = eduPracticeService.findPage(new Page<EduPractice>(request, response), eduPractice);
		model.addAttribute("page", page);
		model.addAttribute("sectionId",sectionId);
		model.addAttribute("title", eduPractice.getTitle());
		return "modules/edu/practice/eduPracticeList";
	}

	@RequiresPermissions("edu:practice:eduPractice:view")
	@RequestMapping(value = "form")
	public String form(EduPractice eduPractice, Model model, HttpServletRequest request, HttpServletResponse response) {
		String sectionId = request.getParameter("sectionId");
		EduPracticSubject eduPracticSubject = new EduPracticSubject();
		eduPracticSubject.setPracticeId(eduPractice.getId());
		//根据习题id查出习题题目关联表，再查出题目，set进eduPractice
		List<EduPracticSubject> list = eduPracticSubjectService.findList(eduPracticSubject);
		List<EduPracticSubject> list2 = new ArrayList<>();
		for(EduPracticSubject eduPracticSubject2 :list){
			EduSelectionTitle eduSelectionTitle = eduSelectionTitleService.get(eduPracticSubject2.getTitleId());
			eduPracticSubject2.setEduSelectionTitle(eduSelectionTitle);
			list2.add(eduPracticSubject2);
		}
		eduPractice.setPracticSubjectList(list2);
		eduPractice.setSectionId(sectionId);
		model.addAttribute("eduPractice", eduPractice);
		model.addAttribute("sectionId", sectionId);
		return "modules/edu/practice/eduPracticeForm";
	}

	@RequiresPermissions("edu:practice:eduPractice:view")
	@RequestMapping(value = "editPractice")
	public String editPractice(Model model, HttpServletRequest request, HttpServletResponse response){
		String practiceId =request.getParameter("id");
		if(StringUtils.isNotBlank(practiceId)){
			model.addAttribute("practiceId",practiceId);
		}
		model.addAttribute("status",request.getParameter("status"));
		model.addAttribute("sectionId",request.getParameter("sectionId"));
		return "modules/edu/practice/eduPracticeForm";
	}

	@RequiresPermissions("edu:practice:eduPractice:view")
	@RequestMapping(value = "editPracticeGetData")
	public String editPracticeGetData(EduPractice eduPractice, Model model, HttpServletRequest request, HttpServletResponse response){
		BaseResponseMsg baseResponseMsg;
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		//查询practiceSubject
		EduPracticSubject eduPracticSubject = new EduPracticSubject();
		eduPracticSubject.setPracticeId(eduPractice.getId());
		//查询出关联表
		List<EduPracticSubject> practicSubjectObject  = eduPracticSubjectService.findPracticeListByPracticeId(eduPracticSubject);
		for (EduPracticSubject eduPracticSubject1 :practicSubjectObject){
			EduSelectionTitle eduSelectionTitle = eduSelectionTitleService.get(eduPracticSubject1.getTitleId());
			eduPracticSubject1.setEduSelectionTitle(eduSelectionTitle);
		}
		eduPractice.setPracticSubjectList(practicSubjectObject);
		Map map = new HashMap();
		map.put("eduPractice", eduPractice);
		map.put("practicSubjectObject", practicSubjectObject);
		map.put("practiceId", eduPractice.getId());
		map.put("sectionId", eduPractice.getSectionId());
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}

	//保存练习要求
	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "addPracticeRequirements")
	public String addPracticeRequirements(EduPractice eduPractice, HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		System.out.println(eduPractice.getRequirement());
		eduPracticeService.save(eduPractice);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduPractice);
		return renderString(response, baseResponseMsg);
	}


	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(@RequestBody EduPractice eduPractice, HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		List<EduPracticSubject> practicSubjectList = eduPractice.getPracticSubjectList();
		if(StringUtils.isNotBlank(eduPractice.getId())){
			//修改，删除绑定关系
			EduPracticSubject  eduPracticSubject= new EduPracticSubject();
			eduPracticSubject.setPracticeId(eduPractice.getId());
			List<EduPracticSubject>  EduPracticSubjects = eduPracticSubjectService.findPracticeListByPracticeId(eduPracticSubject);
			for(EduPracticSubject eduPracticSubject1:EduPracticSubjects){
				eduPracticSubjectService.delete(eduPracticSubject1);
			}
		}
		eduPracticeService.save(eduPractice);
		for(EduPracticSubject eduPracticSubject : practicSubjectList){
			eduPracticSubject.setPracticeId(eduPractice.getId());
			eduPracticSubjectService.save(eduPracticSubject);
		}
		EduSection eduSection = eduSectionService.get(eduPractice.getSectionId());
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduPractice);
		baseResponseMsg.addData("courseId",eduSection.getCourseId());
		return renderString(response, baseResponseMsg);
	}



	//选为模板  把练习拷贝到新的章节中
	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "copyPracticeToNewSection")
	public String copyPracticeToNewSection(EduPractice eduPractice,HttpServletRequest request, HttpServletResponse response){
		BaseResponseMsg baseResponseMsg;
		//eduPractice 是旧的   模板的联练习内容
		//查询这个练习下的所有的题目
		EduPracticSubject eduPracticSubject = new EduPracticSubject();
		eduPracticSubject.setPracticeId(eduPractice.getId());
		List<EduPracticSubject> eduPracticSubjectList = eduPracticSubjectService.findList(eduPracticSubject);

		eduPractice.setSectionId(request.getParameter("sectionId"));
		eduPractice.setId(null);//触发成一个新的对象
		eduPractice.setRemarks(Constant.PRACTICE_TEMPLATE_NO);//表示不选为模板库
		eduPracticeService.save(eduPractice);
		//保存过后此时的练习对象是一个新的对象
		//List<EduPracticSubject> list = new ArrayList<>();
		//循环把习题题目关联表里的习题id换成现在的顺便把id清空，确保是添加
		for(EduPracticSubject eduPracticSubjectTemplate :eduPracticSubjectList){
			eduPracticSubjectTemplate.setPracticeId(eduPractice.getId());
			eduPracticSubjectTemplate.setId(null);
			eduPracticSubjectService.save(eduPracticSubjectTemplate);
			//list.add(eduPracticSubjectTemplate);
		}
		//eduPracticSubjectService.saveList(list);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduPractice);
		return renderString(response, baseResponseMsg);
	}


	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "updateDemand")
	public String updateDemand(EduPractice eduPractice, Model model, RedirectAttributes redirectAttributes){
		eduPracticeService.save(eduPractice);
		addMessage(redirectAttributes, "保存单元练习模板成功");
		return "redirect:"+Global.getAdminPath()+"/edu/practice/eduPractice/?repage";
	}



	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "delete")
	public String delete(EduPractice eduPractice, RedirectAttributes redirectAttributes) {
		eduPracticeService.delete(eduPractice);
		addMessage(redirectAttributes, "删除单元练习成功");
		return "redirect:"+Global.getAdminPath()+"/edu/practice/eduPractice/?repage";
	}

	//删除模板
	@RequiresPermissions("edu:practice:eduPractice:edit")
	@RequestMapping(value = "delete2")
	public String delete2(EduPractice eduPractice,HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		eduPracticeService.delete(eduPractice);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		return renderString(response, baseResponseMsg);
	}

}